package com.yycx.module.bus.provider.handler.refund;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.interceptor.GetInterceptor;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.bus.client.entity.BusInvoiceunit;
import com.yycx.module.bus.client.entity.BusInvoiceunitBank;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.provider.service.BusInvoiceunitBankService;
import com.yycx.module.bus.provider.service.BusInvoiceunitService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.bus.provider.service.BusSupplierBankService;
import com.yycx.module.file.provider.service.SysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 返款详情扩展
 *
 * @author zyf
 */
@Component("refundPaymentGetHandler")
public class RefundPaymentGetHandler implements GetInterceptor {

    @Autowired
    private BusSupplierBankService supplierBankService;

    @Autowired
    private BusPaymentService paymentService;

    @Autowired
    private BusInvoiceunitService invoiceunitService;

    @Autowired
    private BusInvoiceunitBankService invoiceunitBankService;


    @Autowired
    private SysFileService fileService;
    @Override
    public void prepare(CriteriaQuery cq, EntityMap entityMap) {
        cq.select(BusPayment.class, "*");
    }

    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        Long supplierId = map.getLong("supplierId");
        Long parentId = map.getLong("parentId");
        //查询开票税金
        BusPayment invoicePayment = paymentService.getById(parentId);
        map.put("invoicePayment", invoicePayment);
        if (FlymeUtils.isNotEmpty(invoicePayment)) {
            Long invoiceUnitId = invoicePayment.getInvoiceUnitId();
            BusInvoiceunit invoiceUnit = invoiceunitService.getById(invoiceUnitId);
            map.put("invoiceUnit", invoiceUnit);
            BusInvoiceunitBank invoiceUnitBank = invoiceunitBankService.getByProperty("invoiceUnitId", invoiceUnitId);
            map.put("invoiceUnitBank", invoiceUnitBank);
            Long paymentId = invoicePayment.getPaymentId();
            if (FlymeUtils.isNotEmpty(paymentId)) {
                List<EntityMap> fileList = fileService.selectFileListByFileType(paymentId.toString(), "BusPayment", "");
                map.put("invoicePaymentImage", fileList);
            }
        }
        ResultBody resultBody = supplierBankService.getBySupplierIdAndBankType(supplierId, 2);
        EntityMap supplierBank = (EntityMap) resultBody.getData();
        map.putAll(supplierBank);
    }
}
